<?php
namespace App\Admin\Controllers;

use SmallRuralDog\Admin\Controllers\AdminController;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use SmallRuralDog\Admin\Form;
use SmallRuralDog\Admin\Grid;
use SmallRuralDog\Admin\Auth\Database\Menu;
use SmallRuralDog\Admin\Components\Attrs\SelectOption;
use SmallRuralDog\Admin\Components\Form\IconChoose;
use SmallRuralDog\Admin\Components\Form\InputNumber;
use SmallRuralDog\Admin\Components\Form\Select;
use SmallRuralDog\Admin\Components\Form\Radio;
use SmallRuralDog\Admin\Components\Form\RadioGroup;
use SmallRuralDog\Admin\Components\Grid\Avatar;
use SmallRuralDog\Admin\Components\Grid\Icon;
use SmallRuralDog\Admin\Components\Grid\Tag;
use SmallRuralDog\Admin\Components\Grid\Image;
use SmallRuralDog\Admin\Grid\Actions\ActionButton;
use SmallRuralDog\Admin\Components\Widgets\Dialog;
use SmallRuralDog\Admin\Components\Widgets\BaseForm;
use SmallRuralDog\Admin\Components\Form\Input;
use SmallRuralDog\Admin\Components\Form\Upload;
use SmallRuralDog\Admin\Components\Form\Checkbox;
use SmallRuralDog\Admin\Components\Widgets\Divider;
use SmallRuralDog\Admin\Components\Form\WangEditor;
use SmallRuralDog\Admin\Components\Form\DateTimePicker;
use SmallRuralDog\Admin\Components\Form\DatePicker;
use SmallRuralDog\Admin\Components\Form\CSwitch;
use SmallRuralDog\Admin\Layout\Content;
use App\Models\Activity;
use App\Models\MemberScoreLog;

class ActivityController extends AdminController
{

    protected function grid()
    {
        $grid = new Grid(new Activity());
        $grid->defaultSort('created_at', 'desc');
        $grid->column('id', "活动编号")->width(100)->sortable();
        $grid->column('title', "名称");
        $grid->column('imgurl', "活动图")->width(80)->align('center')->component(Image::make()->size(50, 50)->preview());
        $grid->column('score', "可得积分");
        $grid->column('start_time', "开始时间")->sortable();
        $grid->column('end_time', "结束时间");
        $grid->column('status', "状态")->customValue(function ($row, $value) {
            $status_name = ['ready' => '未开始','start' => '进行中', 'end' => '已结束'];
            return $status_name[$value];
        })->width(100)->sortable()->component(Tag::make()->type(["未开始" => "default", "进行中" => "success", '已结束'=>"info"]));
        $grid->column('apply', "是否要报名")->customValue(function ($row, $value) {
            $apply_name = ['1' => '需要','0' => '不需要'];
            return $apply_name[$value];
        })->width(100);
        $grid->column('check_mobile', "是否验证手机号")->customValue(function ($row, $value) {
            $mobile_name = ['1' => '是','0' => '否'];
            return $mobile_name[$value];
        })->width(120);
        $grid->column('limit_num', "最大可参与人数")->customValue(function ($row, $value) {
            return $value > 0 ? intval($value) : '不限制';
        })->width(120)->align('center');
        $grid->column('join_num', "当前参与人数")->width(120)->align('center');
        $grid->column('qrcode', "活动二维码")->width(120)->align('center')->component(Image::make()->size(50, 50)->preview());

        $grid->column('shelves', "上架状态")->width(100)->align("center")->customValue(function ($row, $value) {
            return $value == 1 ? "上架" : "下架";
        })->component(Tag::make()->type(["上架" => "success", "下架" => "danger"]));


        $grid->actions(function (Grid\Actions $actions){

        })->toolbars(function (Grid\Toolbars $toolbars)
        {
            $toolbars->CreateButton();
        });

        $grid->quickSearch('title');
        $grid->quickSearchPlaceholder("请输入名称");

        return $grid;
    }

    public function form()
    {
        $form = new Form(new Activity());

        $form->labelWidth("250px");
        $form->item('title', "活动名称")->required()->inputWidth(5);
        $form->item('imgurl', '活动图片')->required()->component(Upload::make()->width(150)->height(150));
        $form->item('describe', "活动简介")->inputWidth(10)->help('建议不要超过85字');
        $form->item('score', "获得积分")->required()->inputWidth(2);
        // $form->item('start_time', "开始时间")->component(DateTimePicker::make())->required();
        // $form->item('end_time', "结束时间")->component(DateTimePicker::make())->required();
        $form->item("time", "活动进行时间")->component(DateTimePicker::make(null, 'datetimerange'))->required();
        $form->item('address', "活动地点")->required()->inputWidth(10);
        //$form->item('description', "活动规则")->required()->component(WangEditor::make())->inputWidth(15);
        $form->item('description', "活动详情")->required()->component(WangEditor::make()->uploadImgServer(route('admin.setting.upload'))->showLinkImg(false)->uploadFileName('file[]')->uploadImgMaxSize(20*1024*1024))->topComponent(Divider::make("详细信息"));
        // $form->item('exchange.time', "活动时间")->required()->component(DateTimePicker::make(null, 'datetimerange'))->inputWidth(3);
        $form->item('apply', "是否要报名")->component(RadioGroup::make(1, [
            Radio::make(1, "需要"),
            Radio::make(0, "不需要"),
        ]))->help('不需要报名的活动可以直接扫码签到，需要报名的活动要先报名再签到');
        $form->item('limit_num', '人数限制')->vif('apply', 1)->inputWidth(5)->help('限制活动报名人数，0为不限制');
        $form->item('check_mobile', "验证手机号")->component(RadioGroup::make(0, [
            Radio::make(1, "是"),
            Radio::make(0, "否"),
        ]));
        $form->item('order', '排序')->component(InputNumber::make(1));
        $form->item('shelves', '上架活动')->required()->component(CSwitch::make(0));

        $form->saving(function (Form $form) {
            $activity = $form->model();

            if (!$activity->code)
            {
                $form->code = strtoupper($activity->rand_str(10, false));
            }

            $form->start_time = $form->time[0];
            $form->end_time = $form->time[1];
            unset($form->time);
        });

        return $form;
    }


}
